import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/*
输出流：
从内存到硬盘
 */
public class FileOutputStreamTest01 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            //fos = new FileOutputStream("C:\\Users\\30539\\Desktop\\text2.txt");
            //true说明以追加的形式在原文件之后继续写入，并不会将源文件进行清空
            fos = new FileOutputStream("C:\\Users\\30539\\Desktop\\text2.txt",true);
            byte[] bytes = {97,98,99,100};
            //这种方式会将原文件的内容先清空再重新写入！
            fos.write(bytes);//将byte数组全部写出去
            fos.write(bytes,0,1);//再写一个a
            String s = "小明";
            byte[] bytes1 = s.getBytes(StandardCharsets.UTF_8);
            fos.write(bytes1);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try{
                fos.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
